package com.example.myapplication.activity;

import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import androidx.appcompat.app.AppCompatActivity;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MonthListAdapter;
import com.example.myapplication.bean.MoneyData;
import com.example.myapplication.util.MyDateUtil;
import com.example.myapplication.util.RandomUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author ：xiongwei
 * @date ：Created in 2021/7/18 21:04
 */
public class MonthActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_month_mi10);

        MoneyData moneyData = (MoneyData) getIntent().getSerializableExtra("moneyData");
        List<MoneyData> moneyDataList = getMonthDataList(moneyData);

        ListView listView = findViewById(R.id.month_listview);
        MonthListAdapter monthListAdapter = new MonthListAdapter(this, moneyDataList);
        listView.setAdapter(monthListAdapter);

    }

    private List<MoneyData> getMonthDataList(MoneyData moneyData) {

        List<MoneyData> moneyDataList = new ArrayList<>();

        Date date = MyDateUtil.stringtoDate(moneyData.getDayTime(), MyDateUtil.LONG_DATE_FORMAT);
        Date beginDate = DateUtil.beginOfMonth(date);

        // 30天换算毫秒就是 30*24*3600*1000 = 2,592,000,000
        long startTime = beginDate.getTime() - 2592000000L;

        for (int i = 15; i < 62; i++) {
            moneyDataList.add(createMoneyData(moneyData.getMoney(), startTime, i));
        }
        return moneyDataList;
    }

    private MoneyData createMoneyData(String money, long startTime, int i) {
        // 一天的毫秒数是24*3600*1000 = 86400000
        long addTime = (i + 1) * 86400000L;
        // 取值范围是18：10 到 18：25
        // 18*60+10 = 1090  18*60+10 = 1105
        // 1090*60*1000 = 65400000    1105*60*1000 = 66300000
        int randomTime = RandomUtil.getRandom(66300000, 65400000);
        long dateTime = startTime + addTime + randomTime;
        Date currentDate = new Date(dateTime);
//            String.format("%.2f", money)
        MoneyData moneyData = new MoneyData();
        moneyData.setDate(currentDate);
        moneyData.setDayTime(DateUtil.format(currentDate, DatePattern.NORM_DATE_PATTERN));
        moneyData.setStartTime(DateUtil.format(currentDate, DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        moneyData.setEndTime(DateUtil.format(currentDate, DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        moneyData.setMoney(randomMoney());
        return moneyData;
    }

    private String randomMoney(){
        int bigMoney = RandomUtil.getRandom(36, 25);
        int smallMoney = RandomUtil.getRandom(99, 0);
        return bigMoney+"."+String.format("%02d",smallMoney);
    }

    private String getDate(String time) {
        Date date = MyDateUtil.stringtoDate(time, MyDateUtil.FORMAT_TWO);
        return MyDateUtil.dateToString(date, MyDateUtil.DATE_FORMAT_yyyyMMdd);
    }

}
